package com.yunhai.web.controller.reveal;

import java.util.List;
import java.util.Arrays;

import com.yunhai.common.annotation.Log;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.enums.BusinessType;
import com.yunhai.common.utils.poi.ExcelUtil;
import com.yunhai.system.domain.Product;
import com.yunhai.system.domain.admin.ProductAdminVo;
import com.yunhai.system.domain.vo.ProductVo;
import com.yunhai.system.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 产品展示Controller
 *
 * @author jxj
 * @date 2021-01-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/reveal/product")
public class ProductController extends BaseController {
    private final ProductService productService;

    /**
     * 查询产品展示列表
     */
    @PreAuthorize("@ss.hasPermi('reveal:product:list')")
    @GetMapping("/list")
    public AjaxResult list(Product product) {
        List<ProductAdminVo> productAdminVos = productService.queryList(product);
        return AjaxResult.success(productAdminVos);
    }

    /**
     * 导出产品展示列表
     */
    @PreAuthorize("@ss.hasPermi('reveal:product:export')" )
    @Log(title = "产品展示" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Product product) {
        List<ProductAdminVo> list = productService.queryList(product);
        ExcelUtil<ProductAdminVo> util = new ExcelUtil<ProductAdminVo>(ProductAdminVo.class);
        return util.exportExcel(list, "product" );
    }

    /**
     * 获取产品展示详细信息
     */
    @PreAuthorize("@ss.hasPermi('reveal:product:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(productService.getById(id));
    }

    /**
     * 新增产品展示
     */
    @PreAuthorize("@ss.hasPermi('reveal:product:add')" )
    @Log(title = "产品展示" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        return toAjax(productService.save(product) ? 1 : 0);
    }

    /**
     * 修改产品展示
     */
    @PreAuthorize("@ss.hasPermi('reveal:product:edit')" )
    @Log(title = "产品展示" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateById(product) ? 1 : 0);
    }

    /**
     * 删除产品展示
     */
    @PreAuthorize("@ss.hasPermi('reveal:product:remove')" )
    @Log(title = "产品展示" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
